$(function(){
    $('nav#menu_left').mmenu();
    jQuery('#output').qrcode(siteGlobal.SITE_HOST+'/invite/'+actId);
    $('#act_qrcode').on('click', function(){
        $('#qrcodeDiv').modal('show');
    });
    loadActAttends();
    $('#toJoin').on('click', function(e){
        var btn = $(this);
        if(needSign !== 'false'){
            $('#moreInfo').modal('show');
        }else{
            attendAct(btn);
        }
    });
    $('#toCancel').on('click', function(e){
        AlertBox.showConfirm('<br/>放弃这次好机会？', {ok:'下次去吧',cancel:'一定要去'}, function(){
            var btn = $(this);
            btn.button('loading');
            $.post('/cancel', {id:actId}, function(res){
                if(!res.error){
                    if(res.data == null){
                        AlertBox.showTip('还未参与!');
                    }else{
                        AlertBox.showTip('下次再去吧√');
                        window.location.href = '';
                    }
                }else{
                    AlertBox.showTip('取消失败×')
                }
                btn.button('reset');
            })
        }, function(){
            AlertBox.closeAlertBox();
        })
    });
    $('#toStart').on('click', function(e){
        window.location.href = '/create'
    });
    $('#finish').on('click', function(e){
        var btn = $(this);
        submitInfo(function(){
            attendAct(btn);
            $('#moreInfo').modal('hide');
        });
    });
    $('#loadMore').live('click', function(e){
        var btn = $(this);
        $('.hide_more').each(function(i){
            btn.remove();
            $(this).fadeIn();
        })
    })
});
function submitInfo(callback){
    var mobile = $.trim($('#mobile').val()),
        company = $.trim($('#company').val()),
        business = $.trim($('#business').val())
    $.post('/finishMyInfo', {
        mobile: mobile,
        company: company,
        business: business
    }, function(res){
        if(res.error) return AlertBox.showTip('出问题了T_T');
        callback();
    })
}
function attendAct(btn){
    btn.button('loading');
    var faction = $('#factionId').val();
    $.post('/apply', {id:actId, remark: '我来啦!',faction: faction}, function(res){
        if(!res.error){
            if(res.data === 'already'){
                AlertBox.showTip('已经参与了～');
            }else{
                AlertBox.showTip('参与成功√');
                $('#joinBtnDiv').hide();
                loadActAttends();
            }
        }else{
            AlertBox.showTip(res.error==='outside'?'非此圈内人无法参加':'参与失败');
        }
        btn.button('reset');
    });
}
function loadActAttends(){
    var html = '';
    $.getJSON('/fetchAttends/'+actId, function(res){
        if(res.error) return AlertBox.showTip('加载出错');
        var attends = res.attends;
        attends.forEach(function(attend, index){
            var info = attend.card? attend.card.business+' '+attend.card.product : attend.card.company;
            html += '<li class="'+(index > 5?'hide_more':'')+'">\
                                 <div class="col-xs-2">\
                                    <img src="'+attend.headimg+'" alt="10" class="img-rounded size_32">\
                                 </div>\
                                 <div class="col-xs-10">\
                                    <div><a href="/user/'+attend._id+'">'+attend.name+'</a></div>\
                                    <div style="font-size: 12px;color: #999999">'+info+'</div>\
                                </div>\
                            </li><hr class="'+(index > 5?'hide_more':'')+'"/>'
        });
        html += '<li>\
                            <div id="loadMore">看看还有谁<span class="icon-arrow-down"></span>\
                            </div>\
                        </li>'
        $('#attendlist').html(html);

        if(attends.length > 0){
            if($('#attendDiv').is(':hidden')){
                $('#attendDiv').show();
            }
        }
    })
}